package com.gxa.service.impl;

import com.gxa.common.R;
import com.gxa.mapper.SystemMapper;
import com.gxa.pojo.Log;
import com.gxa.pojo.System_Param;
import com.gxa.pojo.User;
import com.gxa.service.SystemService;
import com.gxa.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 周林森
 * Date: 2021/12/07 18:34:43
 * Description: 高级设置业务
 * Version: V1.0
 */
@Service
public class SystemServiceImpl implements SystemService {

    @Autowired(required = false)
    private SystemMapper systemMapper;

    //查询系统参数
    @Override
    public R querySystem() {
        System_Param system_param = systemMapper.querySystemParam();
        return new R(200,"成功",system_param);
    }

    //修改系统参数
    @Override
    public R updateSystemParam(System_Param system_param) {
        int i = systemMapper.updateSystemParam(system_param);
        return new R(200,"修改成功");
    }

    //登录
    @Override
    public R userLogin(String username, String password) {
        String pwd = MD5Utils.encrypt(password,MD5Utils.MD5_KEY);
        System.out.println(username);
        System.out.println(password);
        System.out.println(pwd);
        User user = systemMapper.queryUser(username,pwd);
        if(user != null) {
            return new R(200, "登录成功", user);
        }
        return new R(500, "账户或用户名错误！");
    }

    @Override
    public R queryUser() {
        List<User> userList = systemMapper.queryUserList();
        return new R(200,"成功",userList);
    }

    @Override
    public R addUser(User user) {
        String pwd = MD5Utils.encrypt(user.getPassword(), MD5Utils.MD5_KEY);
        user.setPassword(pwd);
        int i = systemMapper.addUser(user);
        return new R(200,"添加同事成功");
    }

    @Override
    public R queryLog(Map map) {
        Integer page = (Integer) map.get("page");
        Integer limit = (Integer) map.get("limit");
        if(page == null && limit == null ){
            page = 0;
            limit = 50;
        }else {
            page = (page-1)*limit;
        }
        String name = (String) map.get("name");
        String beginTime = (String) map.get("beginTime");
        String endTime = (String) map.get("endTime");
        List<Log> logList = systemMapper.queryLog(name,beginTime,endTime,page,limit);
        return new R(200,"成功",logList);
    }

    @Override
    public R updatePwd(Map map) {
        Object user_id = map.get("user_Id");
        Object password = map.get("password");
        String pwd = MD5Utils.encrypt((String)password,MD5Utils.MD5_KEY);
        int i = systemMapper.updatePwd(pwd,user_id);
        if(i>0){
            return new R(200,"修改密码成功");
        }else{
            return new R(500,"修改密码失败");
        }
    }
}
